import Vue from 'vue';
let lifeData = {};


// 是否有菜单权限
const hasMenuPermission = userPermission => {
	try{
		lifeData = uni.getStorageSync('lifeData');
	}catch(e){
		
	}
	const permissionList = lifeData['vuex_authList'] ? JSON.parse(lifeData['vuex_authList']) : [];
    let userPermissionList = Array.isArray(userPermission) ? userPermission : [userPermission];
    return userPermissionList.some(e => permissionList.includes(e));
};

// 是否有按钮权限
const hasBtnPermission = userPermission => {
	try{
		lifeData = uni.getStorageSync('lifeData');
	}catch(e){
		
	}
	const btnlist = lifeData['vuex_authList'] ? JSON.parse(lifeData['vuex_authList']) : [];
    let userPermissionList = Array.isArray(userPermission) ? userPermission : [userPermission];
    return userPermissionList.some(e => btnlist.includes(e));
};

//菜单权限指令
Vue.directive('mauth', {
    inserted: (el, binding, vnode) => {
        if (!hasMenuPermission(binding.value) && binding.value) {
			el.parentNode.removeChild(el)
            //el.style.display = 'none';
        }
    }
});
//按钮权限指令
Vue.directive('bauth', {
    inserted: (el, binding, vnode) => {
        if (!hasBtnPermission(binding.value)) {
			el.parentNode.removeChild(el)
            //el.style.display = 'none';
        }
    }
});

// 全局判断方法
Vue.prototype.$_mauth = hasMenuPermission;
Vue.prototype.$_bauth = hasBtnPermission;

